﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_DTTN.TTTS.BUS;
using QL_DTTN.DTO;
using DevExpress.XtraGrid.Views.Grid;
namespace QL_DTTN.TTTS
{
    public partial class FrmCapNhatThongTinThiSinh : DevExpress.XtraEditors.XtraForm
    {
        DataTable dt;
        public FrmCapNhatThongTinThiSinh()
        {
            InitializeComponent();
        }
        private void FrmCapNhatThongTinThiSinh_Load(object sender, EventArgs e)
        {
            cbxTinh_Load();
            cbxHeDaoTao_Load();
            cbxDoiTuongCS_Load();
            cbxKyThi_Load();
            cbxTinh_SelectedIndexChanged(sender, e);
            loadControlGrip();
        }

        private void cbxTinh_Load()
        {
            DataTable dt = Tinh_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maTinh"] = 0;
            d["tenTinh"] = "Tất cả";
            dt.Rows.Add(d);
            cbxTinh.DataSource = dt;
            cbxTinh.DisplayMember = "tenTinh";
            cbxTinh.ValueMember = "maTinh";
            cbxTinh.SelectedIndex = cbxTinh.Items.Count - 1;
        }
        private void cbxTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbxTinh.SelectedIndex != -1 && cbxTinh.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    DataTable dt = Truong_Bus.dataTable(Convert.ToInt32(cbxTinh.SelectedValue));
                    DataRow d = dt.NewRow();
                    d["maTruong"] = 0;
                    d["tenTruong"] = "Tất cả";
                    dt.Rows.Add(d);
                    cbxTruong.DataSource = dt;
                    cbxTruong.DisplayMember = "tenTruong";
                    cbxTruong.ValueMember = "maTruong";
                    cbxTruong.SelectedIndex = cbxTruong.Items.Count - 1;
                }
            }
            catch
            {

            }
        }
        private void loadControlGrip()
        {
            DataTable dt1 = HeDaoTao_Bus.dataTable();
            iluHeDaoTao.DataSource = dt1;
            iluHeDaoTao.DisplayMember = "tenHeDaoTao";
            iluHeDaoTao.ValueMember = "maHeDaoTao";
            DataTable dt2 = DoiTuong_Bus.dataTable();
            iluDoiTuong.DataSource = dt2;
            iluDoiTuong.DisplayMember = "tenDoiTuong";
            iluDoiTuong.ValueMember = "maDoiTuong";
            DataTable dt3 = DanToc_Bus.dataTable();
            iluDanToc.DataSource = dt3;
            iluDanToc.DisplayMember = "tenDanToc";
            iluDanToc.ValueMember = "maDanToc";
            DataTable dt4 = Tinh_Bus.dataTable();
            iluNoiSinh.DataSource = dt4;
            iluNoiSinh.DisplayMember = "tenTinh";
            iluNoiSinh.ValueMember = "maTinh";
        }
        private void cbxHeDaoTao_Load()
        {
            DataTable dt = HeDaoTao_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maHeDaoTao"] = 0;
            d["tenHeDaoTao"] = "Tất cả";
            dt.Rows.Add(d);
            cbxHeDaoTao.DataSource = dt;
            cbxHeDaoTao.DisplayMember = "tenHeDaoTao";
            cbxHeDaoTao.ValueMember = "maHeDaoTao";
            cbxHeDaoTao.SelectedIndex = cbxHeDaoTao.Items.Count - 1;
        }
        private void cbxDoiTuongCS_Load()
        {
            DataTable dt = DoiTuong_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maDoiTuong"] = 0;
            d["tenDoiTuong"] = "Tất cả";
            dt.Rows.Add(d);
            cbxDoiTuongCS.DataSource = dt;
            cbxDoiTuongCS.DisplayMember = "tenDoiTuong";
            cbxDoiTuongCS.ValueMember = "maDoiTuong";
            cbxDoiTuongCS.SelectedIndex = cbxDoiTuongCS.Items.Count - 1;
        }
        private void cbxKyThi_Load()
        {
            DataTable dt = KyThi_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maKyThi"] = 0;
            d["tenKyThi"] = "Tất cả";
            dt.Rows.Add(d);
            cbxKyThi.DataSource = dt;
            cbxKyThi.ValueMember = "maKyThi";
            cbxKyThi.DisplayMember = "tenKyThi";
            cbxKyThi.SelectedIndex = cbxKyThi.Items.Count - 1;
        }
        private void btnTim_Click(object sender, EventArgs e)
        {
            dt = ThiSinh_Bus.getData(txtThongTin.Text.Trim().ToString(), Convert.ToInt16(cbxTinh.SelectedValue), cbxTruong.SelectedValue.ToString(), Convert.ToInt16(cbxHeDaoTao.SelectedValue), cbxDoiTuongCS.SelectedValue.ToString(), Convert.ToInt64(cbxKyThi.SelectedValue));
            gclThiSinh.DataSource = dt.DefaultView;
        }
        private void gvwDanhSachThiSinh_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DTO.DTO.ThiSinh ts = new DTO.DTO.ThiSinh();
            GridView view = sender as GridView;
            ts.SoBaoDanh = view.GetRowCellValue(e.RowHandle, view.Columns["SoBaoDanh"]).ToString();
            ts = ThiSinh_Bus.getThiSinh(ts.SoBaoDanh);
            ts.SoCMND = view.GetRowCellValue(e.RowHandle, view.Columns["SoCMND"]).ToString();
            ts.DiaChi = view.GetRowCellValue(e.RowHandle, view.Columns["DiaChi"]).ToString();
            ts.GioiTinh = (view.GetRowCellValue(e.RowHandle, view.Columns["GioiTinh"]).ToString() == "Nam");
            ts.HoTen = view.GetRowCellValue(e.RowHandle, view.Columns["HoTen"]).ToString();
            ts.MaDanToc = Convert.ToInt16(view.GetRowCellValue(e.RowHandle, view.Columns["MaDanToc"]));
            ts.MaDoiTuong = view.GetRowCellValue(e.RowHandle, view.Columns["MaDoiTuong"]).ToString();
            ts.MaHeDaoTao = Convert.ToInt16(view.GetRowCellValue(e.RowHandle, view.Columns["MaHeDaoTao"]));
            ts.MaTinh = Convert.ToInt16(view.GetRowCellValue(e.RowHandle, view.Columns["MaTinh"]));
            ts.NgaySinh = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns["NgaySinh"]));
            if (view.GetRowCellValue(e.RowHandle, view.Columns["NgayVaoDang"]).ToString().Trim() != "")
            {
                ts.NgayVaoDang = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns["NgayVaoDang"]));
            }
            if (view.GetRowCellValue(e.RowHandle, view.Columns["NgayVaoDoan"]).ToString().Trim() != "")
            {
                ts.NgayVaoDoan = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns["NgayVaoDoan"]));
            }
            if (!ThiSinh_Bus.Sua(ts)) MessageBox.Show("Lỗi");
        }
        private void gclThiSinh_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && gvwDanhSachThiSinh.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Bạn muốn xóa?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    ThiSinh_Bus.Xoa(gvwDanhSachThiSinh.GetRowCellValue(gvwDanhSachThiSinh.FocusedRowHandle, "SoBaoDanh").ToString());
                    GridView view = sender as GridView;
                    dt.Rows.RemoveAt(gvwDanhSachThiSinh.FocusedRowHandle);
                }
            }
        }
        private void gclThiSinh_DoubleClick(object sender, EventArgs e)
        {
            if (gvwDanhSachThiSinh.FocusedRowHandle >= 0)
            {
                FrmThemThiSinh frmSua = new FrmThemThiSinh();
                frmSua.Name = "frmSuaTT";
                frmSua.ThiSinh = ThiSinh_Bus.getThiSinh(gvwDanhSachThiSinh.GetRowCellValue(gvwDanhSachThiSinh.FocusedRowHandle, "SoBaoDanh").ToString());
                frmSua.ShowDialog();
                btnTim_Click(sender, e);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("Bạn có chắc muốn thoát không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (DialogResult == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void ibtnXem_Click(object sender, EventArgs e)
        {
            string soBaoDanh = gvwDanhSachThiSinh.GetRowCellValue(gvwDanhSachThiSinh.FocusedRowHandle, "SoBaoDanh").ToString();
            FrmXemThongTinThiSinh frm = new FrmXemThongTinThiSinh();
            frm.ThiSinh = BUS.ThiSinh_Bus.getThiSinh(soBaoDanh);
            frm.ShowDialog();
        }

        //private void simpleButton1_Click(object sender, EventArgs e)
        //{
        //    DataTable ecel = dt.Copy();
        //    ecel.Columns["HoTen"].ColumnName = "Họ Tên";
        //    ecel.Columns[""].ColumnName = "";
        //    DTO.ExcelDataProvider.ExportDataGridViewTo_Excel12("Test", 6, ecel);
        //}
    }
}
